package com.example.yls.demoa;

import android.Manifest;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Button btn_start;
    private Button btn_pause;
    private Button btn_stop;
    private MediaPlayer mediaPlayer;
    private SeekBar progressBar;
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initBunder();
        btn_startEvent();
        btn_pauseEvent();
        btn_stopEvent();
        progressBarEvent();
    }

    private void progressBarEvent() {
              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      while (true){
                          if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                              int total=mediaPlayer.getDuration();
                              int progress=mediaPlayer.getCurrentPosition();
                              int current=100*progress/total;
                              progressBar.setProgress(current);
                              Log.e(TAG, "run: "+current);
                          }

                          try {
                              Thread.sleep(500);
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }


                      }

                  }
              }).start();

              progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                  @Override
                  public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                  }

                  @Override
                  public void onStartTrackingTouch(SeekBar seekBar) {

                  }

                  @Override
                  public void onStopTrackingTouch(SeekBar seekBar) {
                      if (mediaPlayer!=null){
                          int total=mediaPlayer.getDuration();
                          int progress=progressBar.getProgress();
                          int position=total*progress/100;
                          mediaPlayer.seekTo(position);
                      }
                  }
              });
    }

    private void btn_stopEvent() {
                 btn_stop.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View view) {
                         mediaPlayer.stop();
                     }
                 });

    }

    private void btn_pauseEvent() {
             btn_pause.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     mediaPlayer.pause();
                 }
             });


    }

    private void btn_startEvent() {
                btn_start.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if (mediaPlayer == null) {
                            mediaPlayer = new MediaPlayer();

                            try {
                                mediaPlayer.setDataSource("/mnt/shared/Other/shangfeng.aac");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            mediaPlayer.prepareAsync();
                            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                @Override
                                public void onPrepared(MediaPlayer mediaPlayer) {
                                    mediaPlayer.start();
                                }
                            });
                        }
                        else {
                            mediaPlayer.start();
                        }
                    }

                });



    }

    private void initBunder() {
              btn_start=findViewById(R.id.btn_start);
              btn_pause=findViewById(R.id.btn_pause);
              btn_stop=findViewById(R.id.btn_stop);
              progressBar=findViewById(R.id.progress_seekbar);
    }
}
